﻿//$$---- Form CPP ----
//---------------------------------------------------------------------------
#define NO_WIN32_LEAN_AND_MEAN
#include <vcl.h>
#pragma hdrstop

#include "Main.h"
#include <shlobj.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "TntMenus"
#pragma link "TntComCtrls"
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
        int MainFormMaxWidth = GetSystemMetrics(SM_CXSCREEN);
        int MainFormMaxHeight = GetSystemMetrics(SM_CYSCREEN);
        bool bWelcome;
        MainForm->Left = (MainFormMaxWidth - MainForm->Width) / 2;
        MainForm->Top = (MainFormMaxHeight - MainForm->Height) / 2;

        OldSize = MainForm->Width;
        MainLeftPanel->Width = MainForm->Width / 2;
        MainPath = ExtractFilePath(Application->ExeName);
        SetDefault sd;
		bWelcome = sd.FirstRun();
		isInsert = false;

        OldSelectLanguageMenu = MntFile;
        LoadDefaultSetting();
        if(bWelcome == true)
        {
                defaultSetting.DefaultLanguage = CheckDefaultLang();
        }
        SetSplitter();
        SetDriversForList(GetDrivers(),GetDefaultDrivers(GetDrivers(),0),DriverLeftComboBox);
        SetDriversForList(GetDrivers(),GetDefaultDrivers(GetDrivers(),1),DriverRightComboBox);
        SetLanguageList();
        ShowFileToListBox(0);
        ShowFileToListBox(1);

        if(bWelcome == true)
        {
                ShowMessage(defaultMenu.MsgWelcome);
        }
		OldDrivers = GetDrivers();
		ValueScrollBar->Position = 100 - GetVolume( 0 );

		initWinAMP();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormResize(TObject *Sender)
{
        SetSplitter();
		FormResize(ListLeftPanel,MainLeftPanel);
		FormResize(ListRightPanel,MainRightPanel);
		ListBoxResize(FileLeftListBox);
		ListBoxResize(FileRightListBox);
		PlayTrackBar->Width = PlayPanel->Width - PlayTrackBar->Left;
		//MoveWinamp();
		//fvc->setOldSize(MainForm->Width);
}
//---------------------------------------------------------------------------
void TMainForm::FormResize(TPanel *Penel1,TPanel *Penel2)
{
        Penel1->Width = Penel2->Width;
        Penel1->Height = Penel2->Height - Penel1->Top;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::MainSplitterMoved(TObject *Sender)
{
        if(MainSplitter->Left < 20)
        {
                MainSplitter->Left = 20;
        }
        FormResize(ListLeftPanel,MainLeftPanel);
        FormResize(ListRightPanel,MainRightPanel);
        ListBoxResize(FileLeftListBox);
        ListBoxResize(FileRightListBox);
}
//---------------------------------------------------------------------------
void TMainForm::SetSplitter()
{
		DWORD iWidth;
		if((MainLeftPanel->Width == 0)||(OldSize / MainLeftPanel->Width == 0))
		{
            	return ;
        }
        iWidth = MainForm->Width / (OldSize / MainLeftPanel->Width);
        if(iWidth >= MainForm->Width - 20)
        {
                iWidth = MainForm->Width - 20;
		}
        MainLeftPanel->Width = iWidth;
        OldSize = MainForm->Width;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::MntExitClick(TObject *Sender)
{
        Application->Terminate();
}
//---------------------------------------------------------------------------
vector<string> TMainForm::GetDrivers()
{
/*
Public Const DRIVE_CDROM = 5
Public Const DRIVE_FIXED = 3
Public Const DRIVE_RAMDISK = 6
Public Const DRIVE_REMOTE = 4
Public Const DRIVE_REMOVABLE = 2
*/

        DWORD dwDriveList = ::GetLogicalDrives ();
        vector<string> result;
        UINT index = 0;
        char s[3];

        s[1] = ':';
        s[2] = '\0';

        index += 2;
        dwDriveList >>= 2;

        while (dwDriveList)
        {
                if(dwDriveList & 1)
                {
                        s[0] = 0x41 + index;

                        //if((::GetDriveType(s) == 3)||(::GetDriveType(s) == 2))
                        {
                                result.push_back(s);
                        }
                }

                index ++;
                dwDriveList >>= 1;
        }

        return result;
}
//---------------------------------------------------------------------------
void TMainForm::SetDriversForList(vector<string> vs,UINT index,TComboBox *tlb)
{
        vector<string>::iterator vsi;
        string ss;

        tlb->Items->Clear();
        for(vsi = vs.begin();vsi != vs.end();vsi ++)
        {
                ss = *vsi;
                tlb->Items->Add(AnsiString(vsi->c_str()));
        }
        tlb->ItemIndex = index;
}
//---------------------------------------------------------------------------
void TMainForm::SetLanguageList()
{
	TSearchRec sr;
	String sTemp;
	int iAttributes = 0,iPos;
	TTntMenuItem *tmi;
	String DefLan = CheckDefaultLang();

	iAttributes |= faReadOnly * 1;
	iAttributes |= faHidden * 1;
	iAttributes |= faSysFile * 1;
	iAttributes |= faVolumeID * 1;
	iAttributes |= faDirectory * 1;
	iAttributes |= faArchive * 1;
	iAttributes |= faAnyFile * 1;

	if(DefLan == "English")
	{
		if (FindFirst(MainPath + "\\Language\\*.xml", iAttributes, sr) == 0)
		{
			do
			{
				//if ((sr.Attr & iAttributes) == sr.Attr)
				{

					sTemp = sr.Name;
					iPos = sTemp.Pos(".");
					sTemp = sTemp.SubString(0,iPos - 1);
					tmi = new TTntMenuItem(this);
					tmi->Caption = getLangMenu(sTemp);
					tmi->OnClick = LanguageClick;
					if(sTemp == defaultSetting.DefaultLanguage)
					{
						LanguageClick(tmi);
					}
					MainMenu->Items->Items[2]->Add(tmi);
				}
			} while (FindNext(sr) == 0);
			FindClose(sr);
		}
	}
	else
	{
		tmi = new TTntMenuItem(this);
		tmi->Caption = "English";
		tmi->OnClick = LanguageClick;
		LanguageClick(tmi);
		MainMenu->Items->Items[2]->Add(tmi);

		tmi = new TTntMenuItem(this);
		tmi->Caption = DefLan;
		tmi->OnClick = LanguageClick;
		if(sTemp == defaultSetting.DefaultLanguage)
		{
			LanguageClick(tmi);
		}
		MainMenu->Items->Items[2]->Add(tmi);
	}
}
//---------------------------------------------------------------------------
WideString TMainForm::getLangMenu(String inStr)
{
	WideString result;

	if ( inStr == "Tschechei" ) {
		result = L"Česky";
	}
	else if ( inStr == "Slowenien" ) {
		result = L"Slovenský";
    }
	else if ( inStr == "Portugal" ) {
		result = L"&Português";
    }
	else if ( inStr == "Svenska" ) {
		result = L"Svenska";
	}
	else if ( inStr == "Polen" ) {
		result = L"Polski";
	}
	else if ( inStr == "Griechenland" ) {
		result = L"&Eλληνικά";
	}
	else if ( inStr == "Kroatien" ) {
		result = L"Hrvatski";
	}
	else if ( inStr == "Spanien" ) {
		result = L"Español";
	}
	else if ( inStr == "Ungarn" ) {
		result = L"Magyarul";
	}
	else if ( inStr == "Slowakei" ) {
		result = L"Slovenčina";
	}
	else if ( inStr == "Finnland" ) {
		result = L"Suomi";
	}
	else if ( inStr == "Slowakwi" ) {
    	result = L"Slovenščina";         
	}
	else
	{
		result = inStr;
    }

	return result;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::LanguageClick(TObject *Sender)
{
		int i;
		String sTemp;
		WideString sTemp1;
		TTntMenuItem *tmi = (TTntMenuItem *)Sender;
        LoadXMLFile lXMLf;

		sTemp1 = tmi->Caption;
		i = sTemp1.Pos("&");
		sTemp1.Delete(i,1);
		sTemp = getLangFile(sTemp1);

        if(lXMLf.LoadProject(MainPath + "\\Language\\" +sTemp + ".xml") == true)
        {
                if(lXMLf.GetMenus(MainMenu) == true)
                {
						lXMLf.GetMenus(MainLeftPopupMenu);
						lXMLf.GetMenus(MainRightPopupMenu);
//                        defaultMessage = lXMLf.GetMessage();
						lXMLf.GetListView(FileLeftListBox);
                        lXMLf.GetListView(FileRightListBox);
						lXMLf.GetMessage(&defaultMenu);

                        OldSelectLanguageMenu->Checked = false;
                        tmi->Checked = true;
						OldSelectLanguageMenu = tmi;

						defaultSetting.DefaultLanguage = sTemp;
						MainForm->Caption = defaultMenu.MsgTitel;
						tmi->Caption = getLangMenu(sTemp);
                }
        }
}
//---------------------------------------------------------------------------
String TMainForm::getLangFile(WideString inStr)
{
	String result;
	result = inStr;

	int index;
	if ( ( index = result.Pos("(")) > 1 ) {
		result = result.SubString( 0, index - 1 );
	}

	if ( result == "Česky" ) {
		result = "Tschechei";
	}
	else if ( result == "Slovenský" ) {
		result = "Slowenien";
    }
	else if ( result == "Português" ) {
		result = "Portugal";
	}
	else if ( result == "Svenska" ) {
		result = "Svenska";
	}
	else if ( result == "Polski" ) {
		result = "Polen";
	}
	else if ( result == "Eλληνικά" ) {
		result = "Griechenland";
	}
	else if ( result == "Hrvatski" ) {
		result = "Kroatien";
	}
	else if ( result == "Español" ) {
		result = "Spanien";
	}
	else if ( result == "Magyarul" ) {
		result = "Ungarn";
	}
	else if ( result == "Slovenčina" ) {
		result = "Slowakei";
	}
	else if ( result == "Suomi" ) {
		result = "Finnland";
	}
	else if ( result == "Slovenščina") {
       	result = "Slowakwi";
	}
	else
	{
		result = inStr;
    }


	return result;
}
//---------------------------------------------------------------------------
void TMainForm::ListBoxResize(TTntListView *tlv)
{
        int iWidth = 0;

        iWidth = tlv->Width -
                (tlv->Columns->Items[1]->Width + tlv->Columns->Items[2]->Width
                 + tlv->Columns->Items[3]->Width + 4);
	 /*
        if(iWidth < (tlv->Columns->Items[1]->Width + tlv->Columns->Items[2]->Width
                 + tlv->Columns->Items[3]->Width + 4))
        {
                iWidth = (tlv->Columns->Items[1]->Width + tlv->Columns->Items[2]->Width
                 + tlv->Columns->Items[3]->Width + 4);
        } */
        tlv->Columns->Items[0]->Width = iWidth;
}
//---------------------------------------------------------------------------
void TMainForm::LoadDefaultSetting()
{

        LoadXMLFile lXMLf;
        String sTemp;
        if(lXMLf.LoadProject(MainPath + "\\setting.xml") == true)
        {
                if(lXMLf.GetSetting(&defaultSetting) == false)
                {
						defaultSetting.DefaultLanguage = "English";
                        defaultSetting.LeftPath = "C:";
						defaultSetting.RightPath = "C:";
						defaultSetting.FileTime = "1";
                }
                if(ParamStr(1) != "")//如果是自动运行的话会有这个参数的
				{
						if(ExtractFileExt(ParamStr(1)).UpperCase() == ".DVS")
						{
								defaultSetting.LeftPath = ExtractFilePath(ParamStr(1));
								return;
                        }
						//if(ParamStr(1)  )
						sTemp = ParamStr(1) + "\\system.cfg";//检测有没有DVS的目录
						if(FileExists(sTemp) == true)
                        {
								defaultSetting.LeftPath = sTemp;//有DVS的话，就是笔
								isInsert = true;
						}
                        else
                        {
                                defaultSetting.LeftPath = ParamStr(1);
                        }
						sTemp = ParamStr(1) + "\\dvs";//检测有没有DVS的目录
						if(DirectoryExists(sTemp) == true)
                        {
                                defaultSetting.LeftPath = sTemp;//有DVS的话，就是笔
								isInsert = true;
						}
                        else
                        {
                                defaultSetting.LeftPath = ParamStr(1);
                        }
				}
		}
		MainMediaPlayer->TimeFormat=tfHMS;
}
//---------------------------------------------------------------------------
void TMainForm::SaveDefaultSetting()
{

        LoadXMLFile lXMLf;


        if(lXMLf.SetSetting(MainPath + "\\setting.xml",defaultSetting) == true)
        {
        }
}
//---------------------------------------------------------------------------
UINT TMainForm::GetDefaultDrivers(vector<string> driversName,UINT state)
{
        UINT result;
        String defaultDriver,sTemp;
        vector<string>::iterator vsi;

        result = 0;


        if(state == 0)
        {
                if(DirectoryExists(GetRealPath(state)) == false)
                {
                        defaultSetting.LeftPath = driversName[0].c_str();
                        return result;
                }
                defaultDriver = defaultSetting.LeftPath;
        }
        else
        {
                if(DirectoryExists(GetRealPath(state)) == false)
                {
                        defaultSetting.RightPath = driversName[0].c_str();
                        return result;
                }
                defaultDriver = defaultSetting.RightPath;
        }

        defaultDriver = defaultDriver.SubString(0,defaultDriver.Pos(":"));
        defaultDriver = defaultDriver.UpperCase();

        for(vsi = driversName.begin();vsi != driversName.end();vsi ++,result ++)
        {
                sTemp = String(vsi->c_str());
                sTemp = sTemp.UpperCase();
                if(sTemp == defaultDriver)
                {
                        break;
                }
        }

        if(vsi == driversName.end())
        {
                result = 0;
        }

        return result;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::DriverComboBoxChange(TObject *Sender)
{
		TComboBox *tlb = (TComboBox *)Sender;
        String sTemp = tlb->Items->Strings[tlb->ItemIndex];
        if(tlb->Name.Pos("Left") > 0)
        {
				defaultSetting.LeftPath = sTemp;
				SelectFile = defaultSetting.LeftPath;
				ShowFileToListBox(0);
                FileLeftListBox->SetFocus();
        }
        else
        {
				defaultSetting.RightPath = sTemp;
				SelectFile = defaultSetting.RightPath;
				ShowFileToListBox(1);
				FileRightListBox->SetFocus();
        }

}
//---------------------------------------------------------------------------
void TMainForm::ShowFileToListBox(int State)
{
		UINT i;
		TListItem *myItem;
		TTntListView *tlv;
		vector<FileState> vfs;

		vfs = GetWaveAndFolder(State);

		MainTimer->Enabled = false;
		MainMediaPlayer->Close();
		PlayTrackBar->Position = 0;
		PlayTrackBar->Enabled = false;

        if(State == 0)
        {
				tlv = FileLeftListBox;
        }
        else
        {
				tlv = FileRightListBox;
        }
        tlv->Items->Clear();

		for(vector<FileState>::iterator vsfi = vfs.begin();vsfi != vfs.end();vsfi ++)
        {
				if(vsfi->Name == ".")
				{
                    	continue;
				}
				if((GetRealPath(State).Pos("\\") == 0) && (vsfi->Name == ".."))
				{
						continue;
                }

				myItem = tlv->Items->Add();

				myItem->Caption = vsfi->Name;
				myItem->SubItems->Add(vsfi->RefState);
				if(vsfi->Size != 0)
                {
                        myItem->SubItems->Add(vsfi->Size);
                        myItem->ImageIndex = -1;
                }
                else
                {
                        int i = myItem->SubItems->Add("");
                }
                myItem->SubItems->Add(vsfi->Date);
        }
        PathLeftLabel->Caption = GetRealPath(0);
		PathRightLabel->Caption = GetRealPath(1);
		MoveWinamp();
}
//---------------------------------------------------------------------------
vector<FileState> TMainForm::GetWaveAndFolder(int State)
{
        vector<FileState> result;
		vector<FileState> tmpFile;
        FileState fs;
		TSearchRec sr,sr1;
        String sTemp;
		int iAttributes = 0,iPos;
        bool Info;
        TMenuItem *tmi;
		iAttributes |= faReadOnly * 1;
		iAttributes |= faHidden * 1;
		iAttributes |= faSysFile * 0;
		iAttributes |= faVolumeID * 1;
		iAttributes |= faDirectory * 0;
		iAttributes |= faArchive * 1;
		iAttributes |= faAnyFile * 1;

        if(State == 0)
        {
                sTemp = defaultSetting.LeftPath + "\\";
        }
        else
        {
                sTemp = defaultSetting.RightPath + "\\";
        }

		if (FindFirst(sTemp + "*.*", iAttributes, sr) == 0)
		{
			do
			{
					if ((sr.Attr & iAttributes) == sr.Attr)
					{
					if(DirectoryExists(sTemp + sr.Name) == true)
								{
										if (FindFirst(sTemp + sr.Name + "\\*.*", iAttributes, sr1) != 0)
										{
												FindClose(sr1);
                                                continue;
                                        }
                                        FindClose(sr1);

                                        fs.Name = sr.Name;
                                        fs.Size = sr.Size;
										//








										fs.Date = getDate( sr.FindData );
                                        result.push_back(fs);
                                }
			}
		} while (FindNext(sr) == 0);
		FindClose(sr);
	}

        if(State == 0)
        {
                sTemp = defaultSetting.LeftPath + "\\*.dvs";
                fileInfo.clear();
        }
        else
        {
				sTemp = defaultSetting.RightPath + "\\*.wav";
        }

	if (FindFirst(sTemp, iAttributes, sr) == 0)
	{
		do
		{
			if ((sr.Attr & iAttributes) == sr.Attr)
			{
								fs.Name = sr.Name;
								fs.RefState = GetRef(fs.Name,State,&Info);
                                if(Info == true)
                                {
                                        fileInfo[fs.Name] = true;
                                }
								fs.Size = sr.Size;           
								fs.Date = getDate( sr.FindData );
                                tmpFile.push_back(fs);
			}
		} while (FindNext(sr) == 0);
		FindClose(sr);
	}
/*
        for(vector<FileState>::iterator vfi = tmpFile.begin(); vfi != tmpFile.end();vfi ++)
        {
                if(fileInfo[vfi->Name] == true)
                {
                        result.push_back(*vfi);
                }
        }
*/
		sort(tmpFile.begin(),tmpFile.end());

		for(vector<FileState>::iterator vfi = tmpFile.begin(); vfi != tmpFile.end();vfi ++)
		{
                //if(fileInfo[vfi->Name] == false)
                {
                        result.push_back(*vfi);
                }
        }

        return result;
}
//---------------------------------------------------------------------------
String TMainForm::getDate(WIN32_FIND_DATA ffd)
{
	return getDate(ffd, 0);
}
//---------------------------------------------------------------------------
String TMainForm::getDate(WIN32_FIND_DATA ffd, int intType)
{
		FILETIME    lft,   Time;
		WORD   fd,   ft;   

		  switch   (intType)
		  {
                  case   0:   
								  Time   =   ffd.ftCreationTime;//创建时间；
                                  break;   
                  case   1:   
                                  Time   =   ffd.ftLastWriteTime;//最后修改时间；   
                                  break;   
                  case   2:   
                                  Time   =   ffd.ftLastAccessTime;//最后访问时间；   
                                  break;   
                  default:   
    
							  Time   =   ffd.ftCreationTime;
		  }
											FileTimeToLocalFileTime(&Time,   &lft);
				  //转换FILETIME格式成为DOSTIME格式；
		FileTimeToDosDateTime(&lft,   &fd,   &ft);
		return   FileDateToDateTime(MAKELONG(ft,   fd));
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FileListBoxDblClick(TObject *Sender)
{
        String sTemp;
        TListView *tlv = (TListView*) Sender;

        if((!tlv->Selected) || (tlv->Selected->Caption == "."))
                return;

        if(tlv->Name.Pos("Left") > 0)
        {
                sTemp = defaultSetting.LeftPath + "\\" + tlv->Selected->Caption;
                if(DirectoryExists(sTemp) == true)
                {
                        defaultSetting.LeftPath = sTemp;
                        ShowFileToListBox(0);
                }
                else
                {
                        ConvertClick(Sender);
                }
        }
        else
        {
                sTemp = defaultSetting.RightPath + "\\" + tlv->Selected->Caption;
                if(DirectoryExists(sTemp) == true)
                {
						defaultSetting.RightPath = sTemp;
                        ShowFileToListBox(1);
                }
                else
				{
						PlayMenuClick( Sender );
						//ShellExecute(NULL, "open", sTemp.c_str(), NULL, NULL, SW_SHOW);
                }
        }
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FileListBoxKeyPress(TObject *Sender,
      char &Key)
{
        TListView *tlv = (TListView*) Sender;
        if(Key == '\r')
        {
                FileListBoxDblClick(Sender);
        }
        if(Key == '\b')
        {
				if((tlv->Items->Item[0]) && (tlv->Items->Item[0]->Caption == ".."))
				{
                        tlv->Selected = tlv->Items->Item[0];
                        FileListBoxDblClick(Sender);
                }
                /*
                if(tlv->Name.Pos("Left") > 0)
                {
                        defaultSetting.LeftPath += "\\..";
                        FileListBoxDblClick(Sender);
                }
                else
                {
                        defaultSetting.RightPath += "\\..";
                        FileListBoxDblClick(Sender);
                }        */
        }
        PathLeftLabel->Caption = GetRealPath(0);
        PathRightLabel->Caption = GetRealPath(1);
}
//---------------------------------------------------------------------------
String TMainForm::GetRealPath(int State)
{
		String result;

        if(State == 0)
        {
                result = defaultSetting.LeftPath;
        }
        else
        {
                result = defaultSetting.RightPath;
		}
		result = GetRealPath(result);

		return result;
}
//---------------------------------------------------------------------------
String TMainForm::GetRealPath(String result)
{
		int iPos;
		String sTemp1,sTemp2;

		while((iPos = result.Pos("..")) > 0)
		{
                sTemp1 = result.SubString(0,iPos - 2);
                sTemp2 = result.SubString(iPos + 2,result.Length() - iPos);
                iPos = sTemp1.LastDelimiter("\\");
                sTemp1 = sTemp1.SubString(0,iPos - 1);
                result = sTemp1 + sTemp2;
		}
		return result;
}
void __fastcall TMainForm::FileLeftListBoxEnter(TObject *Sender)
{
//        MntConvert->Enabled = true;
//        MntPlay->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FileLeftListBoxExit(TObject *Sender)
{
//        MntConvert->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FileRightListBoxEnter(TObject *Sender)
{
//        MntPlay->Enabled = true;
//        MntConvert->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FileRightListBoxExit(TObject *Sender)
{
//        MntPlay->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::PlayMenuClick(TObject *Sender)
{
		String sTemp;
        
        if(FileRightListBox->Selected == NULL)
		{
                return;
        }

		sTemp = defaultSetting.RightPath + "\\" + FileRightListBox->Selected->Caption;
		MainMediaPlayer->FileName = sTemp;
		MainMediaPlayer->Open();
		PlayTrackBar->Max = MainMediaPlayer->Length;
		//PlayTrackBar->Enabled = true;
		MainTimer->Enabled = true;
		MainMediaPlayer->Enabled = true;
		MainMediaPlayer->Play();
		  MainMediaPlayer->VisibleButtons = MainMediaPlayer->VisibleButtons >> btPlay;
		  MainMediaPlayer->VisibleButtons = MainMediaPlayer->VisibleButtons << btPause;
		


		/*
		if(DirectoryExists(sTemp) == false)
		{
				ShellExecute(NULL, "open", sTemp.c_str(), NULL, NULL, SW_SHOW);
		}
		*/
	MoveWinamp();

}
//---------------------------------------------------------------------------
void __fastcall TMainForm::CopyClick(TObject *Sender)
{
		if(FileExists(SelectFile) == true)
        {
                CopyFileToClip(GetRealPath(SelectFile).c_str());
        }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::DeleteClick(TObject *Sender)
{
        if(DirectoryExists(SelectFile) == true)
        {
                return;
        }
        else if(FileExists(SelectFile) == true)
        {
                if(MessageDlg(defaultMenu.MsgDelete,mtConfirmation , TMsgDlgButtons() << mbYes << mbNo, 0) == mrYes)
                {
                        if(DeleteFile(SelectFile))
                        {
                                ShowFileToListBox(0);
                                ShowFileToListBox(1);
                        }
                }
        }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::PasteClick(TObject *Sender)
{
		int iPos;
        String sTemp;

		sTemp = GetRealPath(SelectFile);
		if(FileExists(sTemp) == true)
		{
				iPos = sTemp.LastDelimiter("\\");
				if(iPos != 0)
						sTemp = sTemp.SubString(0,iPos);
        }

        if(PasteFileFromClip(sTemp.c_str()) == true)
        {
                ShowFileToListBox(0);
                ShowFileToListBox(1);
        }
}
//---------------------------------------------------------------------------
// 将文件Copy到剪贴板
bool TMainForm::CopyFileToClip(LPCSTR pcFile)
{
	// sFile为拷贝文件名，多个文件以\0分隔，以\0\0结束
    int nLen;
    BYTE *pData;
    HGLOBAL hGbl;
	DROPFILES df;
    if(!pcFile || !pcFile[0])
        return false;
    if(!OpenClipboard(0))
        return false;

    EmptyClipboard();
    df.fNC = false;
    df.fWide = false;
    df.pFiles = sizeof(df);
    df.pt.x = df.pt.y = 0;

    for(nLen=0; pcFile[nLen]!=0; )
        nLen += strlen(pcFile + nLen) + 1;
    nLen += 1; //最后结尾的第二个\0字符
    hGbl = GlobalAlloc(GHND, sizeof(df) + nLen);
    pData = (BYTE*)GlobalLock(hGbl);
    if(pData != NULL)
    {
        memcpy(pData, &df, sizeof(df));
        memcpy(pData + sizeof(df), pcFile, nLen);
        GlobalUnlock(hGbl);
        if(!SetClipboardData(CF_HDROP, hGbl))
        {
            GlobalFree(hGbl);
            hGbl = NULL;
        }
    }
    else if(hGbl != NULL)
    {
        GlobalFree(hGbl);
        hGbl = NULL;
    }
	CloseClipboard();
	return (hGbl != NULL);
}
//---------------------------------------------------------------------------
// 从剪贴板Paste出文件
bool TMainForm::PasteFileFromClip(LPCSTR pcDesDir)
{
	int nLen;
    bool bReturn;
    LPCSTR lpcData;
    HGLOBAL hGbl;
    AnsiString strTemp;
    DROPFILES *pdf;
    SHFILEOPSTRUCT fos;

    if(!pcDesDir || !pcDesDir[0])
        return false;

    if(!OpenClipboard(0))
        return false;
    hGbl = GetClipboardData(CF_HDROP);
    pdf = hGbl? (DROPFILES*)GlobalLock(hGbl): NULL;
    if(pdf == NULL)
    {
        CloseClipboard();
        return false;
    }
    lpcData = (LPCSTR)pdf + pdf->pFiles;

    memset(&fos, 0, sizeof(fos));
    if(pdf->fWide)
    {
        strTemp = WideCharLenToString((wchar_t*)lpcData, GlobalSize(hGbl)-pdf->pFiles);
        fos.pFrom = strTemp.c_str();
    }
    else
        fos.pFrom = lpcData;

    String sTemp = fos.pFrom;
    sTemp = sTemp.SubString(sTemp.LastDelimiter(".") + 1,3);
    sTemp = sTemp.UpperCase();

    if((sTemp != "WAV")&&(sTemp != "DVS"))
    {
        return false;
    }

    fos.wFunc = FO_COPY;
    fos.pTo = pcDesDir;
    bReturn = SHFileOperation(&fos)==0;
    GlobalUnlock(hGbl);
	CloseClipboard();
	return bReturn;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FileListBoxClick(TObject *Sender)
{
		TListView *tlv = (TListView*) Sender;
        if(tlv->Selected != NULL)
        {
				if(tlv->Name.Pos("Left") > 0)
                {
                        SelectFile = defaultSetting.LeftPath + "\\" + tlv->Selected->Caption;
                        SelectFile = SelectFile.UpperCase();
                        if(ExtractFileExt(SelectFile) == ".DVS")
                        {
								MntConvert->Enabled = true;
                                PoLConvert->Enabled = true;
                        }
                        else
                        {
                                MntConvert->Enabled = false;
                                PoLConvert->Enabled = false;
                        }
                }
                else
                {
						SelectFile = defaultSetting.RightPath + "\\" + tlv->Selected->Caption;
                        SelectFile = SelectFile.UpperCase();
                        if(ExtractFileExt(SelectFile) == ".WAV")
                        {
                                MntPlay->Enabled = true;
                                PoRPlay->Enabled = true;
                        }
                        else
                        {
                                MntPlay->Enabled = false;
                                PoRPlay->Enabled = false;
                        }
				}
				ShowRef(tlv, SelectFile);
		}
		else
		{
				if(tlv->Name.Pos("Left") > 0)
				{
						SelectFile = defaultSetting.LeftPath;
				}
				else
				{
						SelectFile = defaultSetting.RightPath;
				}
		}
}
//---------------------------------------------------------------------------
void TMainForm::ShowRef(TListView *tlv, String SelectFile)
{
	String sTemp;
	TSearchRec sr;
	bool Info;

	int iAttributes = 0;
	iAttributes |= faReadOnly * 1;
	iAttributes |= faHidden * 1;
	iAttributes |= faSysFile * 1;
	iAttributes |= faVolumeID * 1;
	iAttributes |= faDirectory * 1;
	iAttributes |= faArchive * 1;
	iAttributes |= faAnyFile * 1;

	if (FindFirst(SelectFile, iAttributes, sr) == 0)
	{
		//do
		{
			if ((sr.Attr & iAttributes) == sr.Attr)
			{
				NameEdit->Text = sr.Name;
				if(tlv->Name.Pos("Left") > 0)
				{
					sTemp = GetRef(sr.Name,0,&Info);
					if(sTemp.Length() > 0)
//					if(Info == true)
					{
						NoEdit->Visible = true;
						NoLabel->Visible = true;
						NoEdit->Text = sTemp;
					}
					else
					{
						NoEdit->Visible = false;
						NoLabel->Visible = false;
						NoEdit->Text = "";
					}
				}
				else
				{
					NoEdit->Visible = false;
					NoLabel->Visible = false;
					NoEdit->Text = "";
				}
				SizeEdit->Text = sr.Size;
				CreateTimeEdit->Text = getDate( sr.FindData, 0 );
				LastWrtieTimeEdit->Text = getDate( sr.FindData, 1 );
				LastAccessTimeEdit->Text = getDate( sr.FindData, 2 );
			}
		} //while (FindNext(sr) == 0);
	}
	FindClose(sr);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::MntOpenClick(TObject *Sender)
{
	   //defaultSetting.LeftPath = OpenPathDlg(defaultMenu.MsgOpen);
	   defaultSetting.LeftPath = OpenPathDlg(GetRealPath(0));
	   ShowFileToListBox(0);
       SetDriversForList(GetDrivers(),GetDefaultDrivers(GetDrivers(),0),DriverLeftComboBox);
	   SelectFile = defaultSetting.LeftPath;
	   FileLeftListBox->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::MntSaveAsClick(TObject *Sender)
{
	   //defaultSetting.RightPath = OpenPathDlg(defaultMenu.MsgSaveAs);
	   defaultSetting.RightPath = OpenPathDlg(GetRealPath(1));
       ShowFileToListBox(1);
       SetDriversForList(GetDrivers(),GetDefaultDrivers(GetDrivers(),1),DriverRightComboBox);
	   SelectFile = defaultSetting.RightPath;
	   FileRightListBox->SetFocus();
}
//---------------------------------------------------------------------------
String TMainForm::OpenPathDlg(String sTitle)
{
/*
		String Result = sTitle;
		if(SelectDirectory(Result, TSelectDirOpts()<< sdAllowCreate << sdPerformCreate << sdPrompt,0))
		{
				return Result;
		}
		else
		{
				return sTitle;
		}

 */

		TCHAR szTitle[] = _T("");// = _T(cTitle);
		TCHAR  szDisplayName[MAX_PATH] = _T("");
		TCHAR  szPath[MAX_PATH] = _T("");

		BROWSEINFO bi;

		strcpy(szTitle,sTitle.c_str());

		bi.hwndOwner = this->Handle;
	  bi.pidlRoot = NULL;
	 bi.lpszTitle = szTitle;
	 bi.pszDisplayName = szDisplayName;
	 bi.ulFlags = BIF_RETURNONLYFSDIRS;
	 bi.lpfn  = NULL;
	 bi.lParam = 0;

	 LPITEMIDLIST pItemIDList = SHBrowseForFolder( &bi );

	 if( pItemIDList )
     {
              SHGetPathFromIDList(pItemIDList,szPath) ;

              IMalloc *pMalloc;
              if( SHGetMalloc( &pMalloc ) != NOERROR )
              {
                       ShowMessage( defaultMenu.MsgFileFolder ) ;
                       return szPath;
              }
              pMalloc->Free( pItemIDList );
              if( pMalloc )
                   pMalloc->Release();
       }

	   return szPath;
                 //*/
  //	   return "";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ConvertClick(TObject *Sender)
{
        SelectFile = SelectFile.UpperCase();
		if((ExtractFileExt(SelectFile) != ".DVS")||
				(FileExists(SelectFile) == false))
        {
                return;
		}
		ConvertingFile(SelectFile);
 }
//---------------------------------------------------------------------------
void TMainForm::ConvertingFile(String SelectFile)
{
        String FileName;
		FileName = ExtractFileName(SelectFile);
        FileName = FileName.SubString(0,FileName.LastDelimiter("."));
        FileName = FileName + "WAV";
        FileName = defaultSetting.RightPath + "\\" + FileName;

        if(FileExists(FileName) == true)
        {
                if(MessageDlg(defaultMenu.MsgOvercast ,mtConfirmation , TMsgDlgButtons() << mbYes << mbNo, 0) == mrNo)
                {
                        return;
                }
        }

        PerSenForm = new TPerSenForm(Application, SelectFile, FileName, defaultMenu);
        PerSenForm->ShowModal();
        delete PerSenForm;                              

        //ShowMessage(defaultMenu.MsgFinish);
        ShowFileToListBox(1);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FileListBoxKeyUp(TObject *Sender,
      WORD &Key, TShiftState Shift)
{
		TListView *tlv = (TListView*) Sender;
        if(tlv->Selected != NULL)
        {
                if(tlv->Name.Pos("Left") > 0)
                {
						SelectFile = defaultSetting.LeftPath + "\\" + tlv->Selected->Caption;
                        SelectFile = SelectFile.UpperCase();
                        if(ExtractFileExt(SelectFile) == ".DVS")
                        {
                                MntConvert->Enabled = true;
                                PoLConvert->Enabled = true;
                        }
                        else
                        {
                                MntConvert->Enabled = false;
                                PoLConvert->Enabled = false;
                        }
                }
                else
                {
                        SelectFile = defaultSetting.RightPath + "\\" + tlv->Selected->Caption;
                        SelectFile = SelectFile.UpperCase();
                        if(ExtractFileExt(SelectFile) == ".WAV")
                        {
                                MntPlay->Enabled = true;
                                PoRPlay->Enabled = true;
                        }
                        else
                        {
                                MntPlay->Enabled = false;
                                PoRPlay->Enabled = false;
                        }
				}
				ShowRef(tlv, SelectFile);
        }

}
//---------------------------------------------------------------------------
void __fastcall TMainForm::MainPopupMenuPopup(TObject *Sender)
{
        TPopupMenu *tpm = (TPopupMenu*) Sender;

		if(tpm->Name.Pos("Left") > 0)
        {
                if(FileLeftListBox->Selected != NULL)
                {
						SelectFile = defaultSetting.LeftPath + "\\" + FileLeftListBox->Selected->Caption;
						SelectFile = SelectFile.UpperCase();
						if(ExtractFileExt(SelectFile) == ".DVS")
                        {
                                MntConvert->Enabled = true;
                                PoLConvert->Enabled = true;
                        }
                        else
                        {
                                MntConvert->Enabled = false;
                                PoLConvert->Enabled = false;
                        }
				}
				else
				{
						SelectFile = defaultSetting.LeftPath.UpperCase();
				}
		}
		else
        {
                if(FileRightListBox->Selected != NULL)
                {
						SelectFile = defaultSetting.RightPath + "\\" + FileRightListBox->Selected->Caption;
                        SelectFile = SelectFile.UpperCase();
                        if(ExtractFileExt(SelectFile) == ".WAV")
                        {
                                MntPlay->Enabled = true;
                                PoRPlay->Enabled = true;
                        }
                        else
                        {
                                MntPlay->Enabled = false;
                                PoRPlay->Enabled = false;
                        }
                }
				else
				{
						SelectFile = defaultSetting.RightPath.UpperCase();
				}
		}
}
//---------------------------------------------------------------------------
String TMainForm::GetRef(String FileName,int State,bool *Info)
{
        String result;
        String RealFileName = GetRealPath(State) + "\\" + FileName;
		result = "";
		DWORD infoBuf;

		if((FileExists(RealFileName)) && (State == 0))
        {
				int iFileHandle;
                int iState;
                char pszBuffer[0x200];
                int StartChar = 0x10c;

                try
                {
                        iFileHandle = FileOpen(RealFileName, fmOpenRead);
                        FileRead(iFileHandle, pszBuffer, 0x200);
						FileClose(iFileHandle);
						char tempChar;

						if(((pszBuffer[0x10a]&0xF0) == 0x20)||((pszBuffer[0x10a]&0xF0) == 0))
						{
								//if((pszBuffer[0x10a]&0xF0) == 0x20)
								{
										for(int i = 0;i < 15;i ++)
										{
												if(pszBuffer[StartChar + i] == 0)
												{
														break;
												}
												tempChar = intToCharList[pszBuffer[StartChar + i] - 1];
												/*
												if( tempChar == 'a' )
												{
														result += '\xEC';
														tempChar = '\xA1';
                                                } */
												result += tempChar;
										}
								}


								if((pszBuffer[0x10a]&0xF0) == 0x20)
								{
										iState = ((pszBuffer[0x10a] & 0xF) << 8) + pszBuffer[0x10b];
										result += String(iState);
								}
						}
						else
						{
								result += '\xFF';
						}
						infoBuf = pszBuffer[0x11e];
						infoBuf <<= 8;
						infoBuf |= pszBuffer[0x11f];
						if(infoBuf != 0)
						{
                                *Info = true;
                        }
                        else
                        {
                                *Info = false;
                        }
                }
                catch(...)
                {
						ShowMessage("Get Ref Fail");
						return "";
                }
		}
		else if(!(FileExists(RealFileName)) && (State == 0))
		{
//				ShowMessage(RealFileName);

        }
        return result;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::MntAboutClick(TObject *Sender)
{
        AboutForm = new TAboutForm(Application);
        AboutForm->Caption = defaultMenu.MsgTitel;
        AboutForm->ShowModal();
        delete AboutForm;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::MntConvertHelpClick(TObject *Sender)
{
        HelpForm = new THelpForm(Application);
        HelpForm->ShowModal();
        delete HelpForm;
}
//---------------------------------------------------------------------------
String TMainForm::CheckDefaultLang()
{
        UINT nLanID = GetSystemDefaultLangID();
        WORD PriLan = PRIMARYLANGID(nLanID);
        String strMessage;
        switch(PriLan)
        {
                case LANG_DANISH:                  strMessage="Dansk";break;
                case LANG_GERMAN:                  strMessage="Deutsch";break;
                case LANG_ENGLISH:                 strMessage="English";break;
                case LANG_FINNISH:                 strMessage="Finnland";break;
                case LANG_FRENCH:                  strMessage="Francais";break;
                case LANG_GREEK:                   strMessage="Griechenland";break;
                case LANG_ITALIAN:                 strMessage="Italiano";break;    
                case LANG_CROATIAN:                strMessage="Kroatien";break;    
                case LANG_DUTCH:                   strMessage="Nederlands";break;  
                case LANG_NORWEGIAN:               strMessage="Norsk";break;       
                case LANG_POLISH:                  strMessage="Polen";break;       
                case LANG_PORTUGUESE:              strMessage="Portugal";break;    
                case LANG_SLOVAK:                  strMessage="Slowakwi";break;    
                case LANG_SLOVENIAN:               strMessage="Slowenien";break;   
                case LANG_SPANISH:                 strMessage="Spanien";break;     
                case LANG_SWEDISH:                 strMessage="Svenska";break;     
                case LANG_CZECH:                   strMessage="Tschechei";break;   
                case LANG_HUNGARIAN:               strMessage="Ungarn";break;      
				default:                           strMessage="English";break;
        }

        if(FileExists(strMessage) == false)
        {
				strMessage="English";
        }

        return strMessage;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
		defaultSetting.LeftPath = GetRealPath(0);
		defaultSetting.RightPath = GetRealPath(1);
		SaveDefaultSetting();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FileLeftListBoxStartDrag(TObject *Sender,
	  TDragObject *&DragObject)
{
		TListView *tlv = (TListView *)Sender;
		DargDVSFileName = defaultSetting.LeftPath + "\\" + tlv->Selected->Caption;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FileRightListBoxDragOver(TObject *Sender,
      TObject *Source, int X, int Y, TDragState State, bool &Accept)
{
		if(ExtractFileExt(DargDVSFileName.UpperCase()) != ".DVS")
		{
				Accept = false;
        }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FileLeftListBoxEndDrag(TObject *Sender,
      TObject *Target, int X, int Y)
{
		ConvertingFile(DargDVSFileName);
		DargDVSFileName = "";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::PathTimerTimer(TObject *Sender)
{
        vector<string> tmpDrivers;
        tmpDrivers = GetDrivers();
        UINT i,j;
		String sTemp;
		static bool isInsert = this->isInsert;

		if(OldDrivers.size() < tmpDrivers.size())
        {
				if(isInsert == true)
				{
					return;
                }
				isInsert = true;
				OldDrivers = tmpDrivers;
				i = tmpDrivers.size() - 1;
				for(vector<string>::reverse_iterator vsi = tmpDrivers.rbegin(); vsi != tmpDrivers.rend(); vsi ++)
				{
						sTemp = vsi->c_str();
						sTemp = sTemp.UpperCase();
						if((::GetDriveType(vsi->c_str()) == 2)&&(DirectoryExists(sTemp + "\\DVS") == true))
						{

								//SetDriversForList(tmpDrivers,i,DriverLeftComboBox);
								defaultSetting.LeftPath = sTemp + "\\DVS";
								j = tmpDrivers.size() - i + 2;
								SetDriversForList(tmpDrivers,i,DriverLeftComboBox);
								ShowFileToListBox(0);
								//OldDrivers = tmpDrivers;
								ShowMessage(defaultMenu.MsgAddPen);
								break;

						}
						else if((::GetDriveType(vsi->c_str()) == 2)&&(FileExists(sTemp + "\\system.cfg") == true))
                        {

                                //SetDriversForList(tmpDrivers,i,DriverLeftComboBox);
                                defaultSetting.LeftPath = sTemp;
                                j = tmpDrivers.size() - i + 2;
                                SetDriversForList(tmpDrivers,i,DriverLeftComboBox);
                                ShowFileToListBox(0);
								//OldDrivers = tmpDrivers;
								ShowMessage(defaultMenu.MsgAddPen);
								break;

                        }
						else if(::GetDriveType(vsi->c_str()) == 2)
                        {
                                SetDriversForList(GetDrivers(),GetDefaultDrivers(GetDrivers(),0),DriverLeftComboBox);
                                SetDriversForList(GetDrivers(),GetDefaultDrivers(GetDrivers(),1),DriverRightComboBox);
                                ShowFileToListBox(0);
								ShowFileToListBox(1);
                        }
                        i --;
                }
        }
        else if(OldDrivers.size() > tmpDrivers.size())
        {
				if(isInsert == false)
				{
					return;
				}
				isInsert = false;
				defaultSetting.LeftPath = "C:";
                SetDriversForList(tmpDrivers,0,DriverLeftComboBox);
				ShowFileToListBox(0);
				OldDrivers = tmpDrivers;

				ShowMessage(defaultMenu.MsgRremove);
        }

		OldDrivers = tmpDrivers;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FileLeftListBoxCustomDrawItem(
	  TCustomListView *Sender, TListItem *Item, TCustomDrawState State,
	  bool &DefaultDraw)
{
		String vCaption;
  		vCaption=Item->Caption;
		if(fileInfo[vCaption] == true)
		{
				((TListView *)Sender)->Canvas->Brush->Color = clRed;
		}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::MainTimerTimer(TObject *Sender)
{
		PlayTrackBar->Position = MainMediaPlayer->Position;
		if(PlayTrackBar->Position == PlayTrackBar->Max)
		{
			MainTimer->Enabled = false;
			MoveWinamp();
        }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::MainMediaPlayerPostClick(TObject *Sender,
	  TMPBtnType Button)
{
		if( Button == btPlay )
		{
				MainTimer->Enabled = true;
		  MainMediaPlayer->VisibleButtons = MainMediaPlayer->VisibleButtons >> btPlay;
		  MainMediaPlayer->VisibleButtons = MainMediaPlayer->VisibleButtons << btPause;
		}
		else if( Button == btPause )
		{
				MainTimer->Enabled = false;
		  MainMediaPlayer->VisibleButtons = MainMediaPlayer->VisibleButtons >> btPause;
		  MainMediaPlayer->VisibleButtons = MainMediaPlayer->VisibleButtons << btPlay;
		}
		else if( Button == btStop )
		{
				MainTimer->Enabled = false;
		  MainMediaPlayer->VisibleButtons = MainMediaPlayer->VisibleButtons >> btPause;
		  MainMediaPlayer->VisibleButtons = MainMediaPlayer->VisibleButtons << btPlay;
		}
		MoveWinamp();
}
//---------------------------------------------------------------------------
void TMainForm::initWinAMP()
{
        //DLL的初始化
		DllInst = 0;//将句柄清0
		String s;
		s = MainPath + "\\VIS_AVS.DLL";
		DllInst = LoadLibrary(s.c_str());//获取句柄
		winampVisGetHeaderType winampVis_Dll;
		winampVis_Dll = (winampVisGetHeaderType)
			GetProcAddress(DllInst,"winampVisGetHeader");//初始化DLL的函数

		winampVisHeader* winampRes;
		winampRes = winampVis_Dll();

		m_WinampMod = winampRes->getModule( 0 );
		m_WinampMod->hwndParent = this->Handle;
		m_WinampMod->hDllInstance = DllInst;
		m_WinampMod->sRate = 128;
		m_WinampMod->nCh = 2;
		m_WinampMod->latencyMs = ShowTimer->Interval;
		m_WinampMod->delayMs = ShowTimer->Interval;

		m_WinampMod->Init( m_WinampMod );

		HWND winHandle;
		winHandle = FindWindow( NULL, "Winamp AVS Editor" );
		PostMessage( winHandle, WM_CLOSE, 0, 0 );

		winHandle = FindWindow( NULL, "Winamp AVS Display" );

		RandWinamp();
		//MoveWinamp();

		ShowTimer->Enabled = true;

		m_TopFrm = new TForm(this);
		m_TopFrm->BorderStyle = bsNone;
		m_TopFrm->Show();

		m_LeftFrm = new TForm(this);
		m_LeftFrm->BorderStyle = bsNone;
		m_LeftFrm->Show();

		m_RightFrm = new TForm(this);
		m_RightFrm->BorderStyle = bsNone;
		m_RightFrm->Show();

		m_BottonFrm = new TForm(this);
		m_BottonFrm->BorderStyle = bsNone;
		m_BottonFrm->Show();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ShowTimerTimer(TObject *Sender)
{
	if( m_WinampMod != NULL )
	{
		for (int i = 0; i < 2; i++ )
		{
			for( int j = 0; j < 576; j ++ )
			{
				//会超界，但不理，随便给人个随机数就是了
				m_WinampMod->spectrumData[ i ][ j ] = rand();
			}
		}

		for (int i = 0; i < 2; i++ )
		{
			for( int j = 0; j < 576; j ++ )
			{
				//会超界，但不理，随便给人个随机数就是了
				m_WinampMod->waveformData[ i ][ j ] = rand();
			}
		}

		m_WinampMod->Render( m_WinampMod );
		HWND winHandle;
		winHandle = FindWindow( NULL, "Winamp AVS Display" );
		EnableWindow( winHandle, false );
    }
}
//---------------------------------------------------------------------------
void TMainForm::RandWinamp()
{
	HWND winHandle;
	winHandle = FindWindow( NULL, "Winamp AVS Display" );
	if( winHandle != NULL && MainTimer->Enabled == true )
	{
		TPoint oldPoss;
		GetCursorPos( &oldPoss );//记住位置

		EnableWindow( winHandle, true );

		//MoveWindow( winHandle, -1000, -1000, 100, 100, true );

		TRect tempRect;
		GetWindowRect( winHandle, &tempRect );

		int X = tempRect.left + 30;
		int Y = tempRect.top + 30;

		SetCursorPos(X,Y);//X，Y为BUTTON的位置
		mouse_event(MOUSEEVENTF_RIGHTDOWN,X,Y,0,0);
		mouse_event(MOUSEEVENTF_RIGHTUP,X,Y,0,0);

		keybd_event( VK_DOWN, 0, 0, 0 );
		keybd_event( VK_DOWN, 0, KEYEVENTF_KEYUP, 0 );
		keybd_event( VK_DOWN, 0, 0, 0 );
		keybd_event( VK_DOWN, 0, KEYEVENTF_KEYUP, 0 );
		keybd_event( VK_DOWN, 0, 0, 0 );
		keybd_event( VK_DOWN, 0, KEYEVENTF_KEYUP, 0 );
		keybd_event( VK_RIGHT, 0, 0, 0 );
		keybd_event( VK_RIGHT, 0, KEYEVENTF_KEYUP, 0 );
		int ranScreen = rand() % 50;
		for( int i = 0; i < ranScreen; i ++ )
		{
			keybd_event( VK_DOWN, 0, 0, 0 );
			keybd_event( VK_DOWN, 0, KEYEVENTF_KEYUP, 0 );
		}
		keybd_event( VK_RETURN, 0, 0, 0 );
		keybd_event( VK_RETURN, 0, KEYEVENTF_KEYUP, 0 );
		//EnableWindow( winHandle, false );

		SetCursorPos( oldPoss.x, oldPoss.y );//复原位置
	}
}
//---------------------------------------------------------------------------
void TMainForm::MoveWinamp()
{
	HWND winHandle;
	winHandle = FindWindow( NULL, "Winamp AVS Display" );
	TPoint tlp = MusicPanel->ClientToScreen( TPoint( ShowPanel->Left, ShowPanel->Top ) );
	if( winHandle != NULL )
	{
		if(MainTimer->Enabled == true)
		{
			MoveWindow( winHandle, tlp.x, tlp.y, ShowPanel->Width, ShowPanel->Height, true );
		}
		else
		{
			MoveWindow( winHandle, tlp.x, tlp.y, 1, 1, true );
		}
	}
	if( m_TopFrm != NULL )
	{
		SetWindowPos( m_TopFrm->Handle, HWND_TOPMOST , tlp.x,
			tlp.y, ShowPanel->Width, 15, SWP_SHOWWINDOW );
	}
	if( m_LeftFrm != NULL )
	{
		SetWindowPos( m_LeftFrm->Handle, HWND_TOPMOST , tlp.x,
			tlp.y, 10, ShowPanel->Height, SWP_SHOWWINDOW );
	}
	if( m_RightFrm != NULL )
	{
		SetWindowPos( m_RightFrm->Handle, HWND_TOPMOST , tlp.x + ShowPanel->Width - 6,
			tlp.y, 6, ShowPanel->Height, SWP_SHOWWINDOW );
	}
	if( m_BottonFrm != NULL )
	{
		SetWindowPos( m_BottonFrm->Handle, HWND_TOPMOST , tlp.x,
			tlp.y + ShowPanel->Height - 6, ShowPanel->Width, 6, SWP_SHOWWINDOW );
	}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::WndProc(TMessage &Message)
{
	switch(Message.Msg)
	{
		case WM_MOVE:
            MoveWinamp();
			break;
	}
	TForm::WndProc(Message);//其他的消息继续传递下去
}
//---------------------------------------------------------------------------
bool   TMainForm::GetVolumeControl(HMIXER   hmixer   ,long   componentType,long   ctrlType,MIXERCONTROL*   mxc)
  {
  MIXERLINECONTROLS   mxlc;   
  MIXERLINE   mxl;   
  mxl.cbStruct   =   sizeof(mxl);   
  mxl.dwComponentType   =   componentType;   
  if(!mixerGetLineInfo((HMIXEROBJ)hmixer,   &mxl,   MIXER_GETLINEINFOF_COMPONENTTYPE))   
  {   
  mxlc.cbStruct   =   sizeof(mxlc);   
  mxlc.dwLineID   =   mxl.dwLineID;   
  mxlc.dwControlType   =   ctrlType;   
  mxlc.cControls   =   1;   
  mxlc.cbmxctrl   =   sizeof(MIXERCONTROL);   
  mxlc.pamxctrl   =   mxc;   
  if(mixerGetLineControls((HMIXEROBJ)hmixer,&mxlc,MIXER_GETLINECONTROLSF_ONEBYTYPE))   
  return   0;   
  else   
  return   1;   
  }   
  return   0;   
  }   
  //---------------------------------------------------------------------------
  long   TMainForm::GetMuteValue(HMIXER   hmixer   ,MIXERCONTROL   *mxc)
  {   
  MIXERCONTROLDETAILS   mxcd;   
  MIXERCONTROLDETAILS_BOOLEAN   mxcdMute;   
  mxcd.hwndOwner   =   0;   
  mxcd.cbStruct   =   sizeof(mxcd);   
  mxcd.dwControlID   =   mxc->dwControlID;   
  mxcd.cbDetails   =   sizeof(mxcdMute);   
  mxcd.paDetails   =   &mxcdMute;   
  mxcd.cChannels   =   1;   
  mxcd.cMultipleItems   =   0;   
  if   (mixerGetControlDetails((HMIXEROBJ)hmixer,   &mxcd,MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE))   
  return   -1;   
  return   mxcdMute.fValue;   
  }   
    
  //---------------------------------------------------------------------------   
  unsigned   TMainForm::GetVolumeValue(HMIXER   hmixer   ,MIXERCONTROL   *mxc)
  {   
  MIXERCONTROLDETAILS   mxcd;   
  MIXERCONTROLDETAILS_UNSIGNED   vol;   vol.dwValue=0;   
  mxcd.hwndOwner   =   0;   
  mxcd.cbStruct   =   sizeof(mxcd);   
  mxcd.dwControlID   =   mxc->dwControlID;   
  mxcd.cbDetails   =   sizeof(vol);   
  mxcd.paDetails   =   &vol;   
  mxcd.cChannels   =   1;   
  if(mixerGetControlDetails((HMIXEROBJ)hmixer,   &mxcd,   MIXER_OBJECTF_HMIXER|MIXER_GETCONTROLDETAILSF_VALUE))   
  return   -1;   
  return   vol.dwValue;   
  }   
    
  //---------------------------------------------------------------------------   
  bool   TMainForm::SetMuteValue(HMIXER   hmixer   ,MIXERCONTROL   *mxc,   bool   mute)
  {   
  MIXERCONTROLDETAILS   mxcd;   
  MIXERCONTROLDETAILS_BOOLEAN   mxcdMute;mxcdMute.fValue=mute;   
  mxcd.hwndOwner   =   0;   
  mxcd.dwControlID   =   mxc->dwControlID;   
  mxcd.cbStruct   =   sizeof(mxcd);   
  mxcd.cbDetails   =   sizeof(mxcdMute);   
  mxcd.paDetails   =   &mxcdMute;   
  mxcd.cChannels   =   1;   
  mxcd.cMultipleItems   =   0;   
  if   (mixerSetControlDetails((HMIXEROBJ)hmixer,   &mxcd,   MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE))   
  return   0;   
  return   1;   
  }   
    
  //---------------------------------------------------------------------------   
    
  bool   TMainForm::SetVolumeValue(HMIXER   hmixer   ,MIXERCONTROL   *mxc,   long   volume)
  {   
  MIXERCONTROLDETAILS   mxcd;   
  MIXERCONTROLDETAILS_UNSIGNED   vol;vol.dwValue   =   volume;   
  mxcd.hwndOwner   =   0;   
  mxcd.dwControlID   =   mxc->dwControlID;   
  mxcd.cbStruct   =   sizeof(mxcd);   
  mxcd.cbDetails   =   sizeof(vol);   
  mxcd.paDetails   =   &vol;   
  mxcd.cChannels   =   1;   
  if(mixerSetControlDetails((HMIXEROBJ)hmixer,   &mxcd,   MIXER_OBJECTF_HMIXER|MIXER_SETCONTROLDETAILSF_VALUE))   
  return   0;   
  return   1;   
  }   
    
    
  //---------------------------------------------------------------------------   
  unsigned   WINAPI   TMainForm::GetVolume(int   dev)//得到设备的音量dev=0主音量，1WAVE   ,2MIDI   ,3   LINE   IN
  {   
    
  long   device;unsigned   rt=0;   
  MIXERCONTROL   volCtrl;   
  HMIXER   hmixer;   
      switch   (dev)   
        {   
          case   1:   
                        device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;   break;   
          case   2:   
                          device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;   break;   
          case   3:   
                      //     device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC;   break;     //   cd   音量   
                      //     device=MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;   break;       //麦克风音量   
                      //     device=MIXERLINE_COMPONENTTYPE_SRC_LINE;   break;                   //PC   扬声器音量   
						  device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC;   break;
    
          default:   
                          device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;   
        }   
    
  if(mixerOpen(&hmixer,   0,   0,   0,   0))   return   0;   
  if(!GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_VOLUME,&volCtrl))   
  return   0;   
  rt=GetVolumeValue(hmixer,&volCtrl)*100/volCtrl.Bounds.lMaximum;   
  mixerClose(hmixer);   
  return   rt;   
    
  }   
    
  //---------------------------------------------------------------------------   

  bool   WINAPI   TMainForm::SetVolume(long   dev,long   vol)//设置设备的音量
  {
  //   dev   =0,1,2       分别表示主音量,波形,MIDI   ,LINE   IN
  //   vol=0-100           表示音量的大小   ,   设置与返回音量的值用的是百分比，即音量从0   -   100，而不是设备的绝对值   
  //   retrun   false   表示设置音量的大小的操作不成功   
  //   retrun   true     表示设置音量的大小的操作成功   
    
  long   device;   
  bool   rc=false;   
  MIXERCONTROL   volCtrl;   
  HMIXER   hmixer;   
  switch   (dev)   
    {   
      case   1:   
                        device=MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT;   break;   
      case   2:   
                        device=MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;   break;   
      case   3:   
                        device=MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC;   break;   
    
      default:   
                        device=MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;   
    }   
    
  if(mixerOpen(&hmixer,   0,   0,   0,   0))   return   0;   
    
  if(GetVolumeControl(hmixer,device,MIXERCONTROL_CONTROLTYPE_VOLUME,&volCtrl))   
      {   
      vol=vol*volCtrl.Bounds.lMaximum/100;   
      if(SetVolumeValue(hmixer,&volCtrl,vol))   
      rc=true;   
      }   
  mixerClose(hmixer);   
  return   rc;   
  }   
    
  //---------------------------------------------------------------------------


void __fastcall TMainForm::ValueScrollBarChange(TObject *Sender)
{
	SetVolume( 0, 100 - ValueScrollBar->Position );
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::RandButtonClick(TObject *Sender)
{
	RandWinamp();
		MoveWinamp();

}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormPaint(TObject *Sender)
{
//		MoveWinamp();
}
//---------------------------------------------------------------------------


void __fastcall TMainForm::MoveTimerTimer(TObject *Sender)
{
	Top -= 1;
	MoveTimer->Enabled = false;
}
//---------------------------------------------------------------------------










